package xupt.se.ttms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import xupt.se.ttms.idao.ISeat;
import xupt.se.ttms.model.Seat;
import xupt.se.util.ConnectionManager;

public class SeatDAO implements ISeat
{

    @Override
    public boolean insert(Seat seat) throws Exception
    {
        boolean result = false;
        if(seat == null)
            return result;

        // 获取Connection
        Connection con = ConnectionManager.getInstance().getConnection();
        PreparedStatement pstmt = null;
        try
        {
            String sql = "insert into seat(studio_id,seat_row,seat_column) values(?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, seat.getStudio_id());
            pstmt.setInt(2, seat.getSeat_row());
            pstmt.setInt(3, seat.getSeat_column());
            pstmt.executeUpdate();
            result = true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            // 关闭连接
            ConnectionManager.close(null, pstmt, con);
            return result;
        }

    }

    @Override
    public boolean delete(int id) throws Exception
    {
        boolean result = false;
        if(id == 0)
            return result;

        // 获取Connection
        Connection con = ConnectionManager.getInstance().getConnection();
        PreparedStatement pstmt = null;
        try
        {
            String sql = "delete from seat where studio_id = ?";
            pstmt.setInt(1, id);
            pstmt = con.prepareStatement(sql);
            pstmt.executeUpdate();
            result = true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            // 关闭连接
            ConnectionManager.close(null, pstmt, con);
            return result;
        }
    }

    @Override
    public boolean updateSeat(int id) throws Exception
    {
        boolean result = false;
        if(id == 0)
            return result;

        // 获取Connection
        Connection con = ConnectionManager.getInstance().getConnection();
        PreparedStatement pstmt = null;
        try
        {
            ResultSet rs = con.prepareStatement("select seat_status from seat where seat_id=" + id)
                              .executeQuery();
            int status = 1;
            if(rs.next())
            {
                status = rs.getInt("seat_status");
                System.out.println("status:" + status);
            }
            String sql = "update seat set seat_status = ? where seat_id = ?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, status == 1 ? 0 : 1);
            pstmt.setInt(2, id);
            pstmt.executeUpdate();
            result = true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            // 关闭连接
            ConnectionManager.close(null, pstmt, con);
            return result;
        }
    }

    public void updateSeat(String[] split) throws Exception
    {
        SeatDAO dao = new SeatDAO();
        for(String string : split)
        {
            dao.updateSeat(Integer.parseInt(string));
        }
    }
}
